import {Node, resources, Prefab} from 'cc';
import {GameManager} from "db://assets/scripts/game/GameManager";
import {PoolManager} from "db://assets/scripts/common/PoolManager";
import {GameCameraController} from "db://assets/scripts/game/GameCameraController";
import {PlayerController} from "db://assets/scripts/game/role/player/PlayerController";
import {EventManager} from "db://assets/scripts/common/EventManager";
import {EventType} from "db://assets/scripts/common/EventType";
import {ModelPaths} from "db://assets/scripts/common/ModelPaths";

/**
 * 游戏玩家代理
 */
export class GamePlayerProxy {
    /**
     * 玩家节点
     */
    private playerNode: Node;
    /**
     * 玩家脚本
     */
    private playerController: PlayerController;
    /**
     * 玩家是否复活
     */
    public revived: boolean = false;

    /**
     * 代理对象
     */
    private proxy: GameManager;

    constructor(proxy: GameManager) {
        this.proxy = proxy;
    }

    /**
     * 创建玩家
     *
     * @private
     * @memberof GameManager
     */
    public createPlayer() {
        resources.load(ModelPaths.player, Prefab, (err: Error, prefab: Prefab) => {
            // 初始化玩家
            this.playerNode = PoolManager.getInstance().getNode(prefab, this.proxy.node);

            // 相机跟随
            let gameCameraController = this.proxy.mainCamera.node.getComponent(GameCameraController);
            gameCameraController.followTarget = this.playerNode;

            // 玩家脚本
            this.playerController = this.playerNode.getComponent(PlayerController);
            this.playerController.init();

            // 预加载箭模型
            this.playerController.preloadArrow(() => {
                EventManager.dispatchEvent(EventType.HIDE_LOADING_PANEL);
            })
        });
    }

    /**
     * 设置玩家节点
     *
     * @param playerNode 玩家节点
     */
    public setPlayerNode(playerNode: Node) {
        this.playerNode = playerNode;
    }

    /**
     * 获取玩家节点
     */
    public getPlayerNode(): Node {
        return this.playerNode;
    }

    /**
     * 设置玩家脚本
     *
     * @param playerController 玩家脚本
     */
    public setPlayerController(playerController: PlayerController) {
        this.playerController = playerController;
    }

    /**
     * 获取玩家脚本
     */
    public getPlayerController(): PlayerController {
        return this.playerController;
    }

    /**
     * 设置玩家是否复活
     *
     * @param revived 是否复活
     */
    public setRevived(revived: boolean) {
        this.revived = revived;
    }

    /**
     * 是否复活
     */
    public getRevived(): boolean {
        return this.revived;
    }
}